import Vue from 'vue'

Vue.filter('floatToPercent', function (value, hasPercent) {
  if (typeof value === 'number') {
    value = value.toString()
  }

  if (value && value.indexOf('%') < 0) {
    let m = 0
    try {
      m += value.split('.')[1].length
    } catch (error) {
      m = 0
    }

    // eslint-disable-next-line no-restricted-properties
    return ((Number(value.replace('.', '')) * 100) / Math.pow(10, m) + (hasPercent ? '%' : ''))
  }

  return value
})

Vue.filter('percentToFloat', function (value) {
  if (typeof value === 'number') {
    value = value.toString()
  }

  if (value) {
    return Number(value.replace(/%/, '')) / 100
  }

  return value
})

Vue.filter('rebateAmount', function (val) {
  if (!val) {
    return ''
  }
  if (!val.cpaRatio && !val.cpsRatio) {
    return ''
  }
  if (!val.negotiable) {
    if (!parseInt(val.cpaRatio)) {
      return `[${val.cpsAmount}]`
    }
    if (!parseInt(val.cpsRatio)) {
      return `[${val.cpaRatio}]`
    }
    return `[${val.cpaRatio}+${val.cpsAmount}]`
  }
  if (!parseInt(val.cpaRatio)) {
    return `[成交金额*${val.cpsRatio}%]`
  }
  if (!parseInt(val.cpsRatio)) {
    return `[${val.cpaRatio}]`
  }
  return `[${val.cpaRatio} + 成交金额* ${val.cpsRatio}%]`
})

Vue.filter('formatPrice', function (val) {
  if (!val) {
    return ''
  }
  if (!val.cpaRatio && !val.cpsRatio) {
    return ''
  }
  if (!val.negotiable) {
    if (!parseInt(val.cpaRatio)) {
      return `[${val.cpsAmount}]`
    }
    if (!parseInt(val.cpsRatio)) {
      return `[${val.cpaRatio}]`
    }
    return `[${val.cpaRatio}+${val.cpsAmount}]`
  }
  if (!parseInt(val.cpaRatio)) {
    return `[成交额*${val.cpsRatio}%]`
  }
  if (!parseInt(val.cpsRatio)) {
    return `[${val.cpaRatio}]`
  }
  return `[${Math.floor(val.cpaRatio)} + 成交额* ${Math.floor(val.cpsRatio)}%]`
})
